/*-------------------------------------------------------------------------
 * 版权所有：	黑客马拉松
 * 作者：		罗毅
 * 联系方式：	whluoyi@gmail.com
 * 创建时间：	2016/10/29 11:23:16
 * 版本号：		v1.0
 * 用途描述：	控制器
 *  -------------------------------------------------------------------------*/

namespace SuperMarieApi.App.Controllers.V1
{
    using FaceppSDK;

    using SuperMarieApi.App.Controllers.Biz;
    using SuperMarieApi.App.Controllers.Bll;
    using SuperMarieApi.App.Controllers.Entity;
    using SuperMarieApi.App.Controllers.Framework.Common;
    using SuperMarieApi.App.Controllers.Framework.DataAccess;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    public class MyFaceHelpController : ApiController
    {
        public static List<MyFileEntity> Model = new List<MyFileEntity>();

        // GET api/MyImg
        public IEnumerable<MyFileEntity> Get()
        {
            return Model;
        }

        //保存前端摄像头base64文件，直接去调用Face++
        // POST api/v1/MyFaceHelp
        public HttpResponseMessage Post([FromBody] MyFileEntity value)
        {
            int fileid = 0;
            List<string> files = AppRootBiz.Base64StringToImage(value.Filebase64);
            if (files.Count == 2)
            {
                MyImgEntity entity = new MyImgEntity();
                entity.FilePath = files[0];
                entity.FileUrl = files[1];
                //entity.Filebase64 = value.Filebase64;
                entity.AddTime = DateTime.Now;
                fileid = MyImgBiz.InsertMyImgObj(entity);
            }


            DetectResult res = AppRootBiz.ReadImage(fileid);
            string allres = StringHelper.GetJSON<DetectResult>(res);
            MySessionEntity entity2 = new MySessionEntity();

            entity2.MyImg_ID = fileid;
            entity2.session_id = res.session_id;
            entity2.url = res.url;
            entity2.img_id = res.img_id;
            entity2.face_id = ((FaceppSDK.Face[])(res.face)) != null ? ((FaceppSDK.Face[])(res.face))[0].face_id : "没找到人像";
            //StringHelper.GetJSON<IList<Face>>(res.face);
            entity2.DetectResult = allres;
            entity2.AddTime = DateTime.Now;
            MySessionBiz.InsertMySessionObj(entity2);

            MyFaceEntity faceEntity = new MyFaceEntity();
            faceEntity.face_id = entity2.face_id;
            MyFaceBiz.InsertMyFaceObj(faceEntity);

            string json = "";
            List<MyMusicLabEntity> musiclist = AppRootBiz.ReadMusic(faceEntity.face_id);
            if (musiclist.Count > 0)
            {
                json = StringHelper.GetJSON<List<MyMusicLabEntity>>(musiclist);
            }
            else
            {
                json = "{_face_id:" + faceEntity.face_id + "}";
            }

            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content =
                    new StringContent(
                    json,
                    System.Text.Encoding.UTF8,
                    "application/json")
            };


        }

    }
}